<template>
  <div class="container">
    <div class="app-container">
      <!-- 添加权限按钮 -->
      <div style="padding: 10px">
        <el-button type="primary" size="mini" @click="clickPermission('add1',null)">添加权限</el-button>
      </div>
      <!-- 展示权限表格 -->
      <el-table :data="permissionList" row-key="id" default-expand-all>
        <el-table-column prop="name" label="名称" />
        <el-table-column prop="code" label="标识" />
        <el-table-column prop="description" label="描述" />
        <el-table-column label="操作">
          <template #default="{row}">
            <el-button v-if="row.type===1" type="text" size="mini" @click="clickPermission('add2',row.id)">添加</el-button>
            <el-button type="text" size="mini" @click="clickPermission('edit',row.id)">编辑</el-button>
            <el-button type="text" size="mini" @click="delPermission(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <addPerm
      ref="addPerm"
      :ids="ids"
      :types="types"
      :dialog-visible.sync="dialogVisible"
      @getPermissionList="getPermissionList"
    />
  </div>
</template>
<script>
import { getPermissionListAPI, delPermissionAPI } from '@/api/permission'
import { listToTreeList } from '@/utils/index'
import addPerm from './components/add-perm.vue'
export default {
  name: 'Permission',
  components: {
    addPerm
  },
  data() {
    return {
      permissionList: [],
      ids: null,
      dialogVisible: false,
      types: ''
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      this.permissionList = listToTreeList(await getPermissionListAPI(), 0)
    },
    clickPermission(type, id) {
      this.types = type
      this.ids = id
      this.dialogVisible = true
      this.$nextTick(() => {
        if (type === 'edit') {
          this.$refs.addPerm.lookPermission()
        }
      })
    },
    async delPermission(id) {
      await delPermissionAPI(id)
      this.getPermissionList()
      this.$message.success('删除成功')
    }
  }
}
</script>
<style lang="scss" scoped>

</style>
